/********************************************************************************************

PNGU Version : 0.1c

Coder : frontier

More info : http://frontier-dev.net

********************************************************************************************/
#ifndef __PNGU__
#define __PNGU__


#ifdef HW_RVL
// Comment the following line if wiisd support is not desired
#define PNGU_USE_WII_SD
#endif


// Return codes
#define PNGU_OK							0
#define PNGU_ODD_WIDTH					1
#define PNGU_ODD_STRIDE					2
#define PNGU_INVALID_WIDTH_OR_HEIGHT	3
#define PNGU_FILE_NOT_PNG				4
#define PNGU_LIB_ERROR					5
#define PNGU_INVALID_BUFFER				6
#define PNGU_PATH_TOO_LONG				7
#define PNGU_NO_FILE_SELECTED			8
#define PNGU_CANT_OPEN_FILE				9
#define PNGU_CANT_READ_FILE				10
#define PNGU_NOT_INITIALIZED			11
#define PNGU_ALREADY_INITIALIZED		12


// Color types
#define PNGU_COLOR_TYPE_GRAY			1
#define PNGU_COLOR_TYPE_GRAY_ALPHA		2
#define PNGU_COLOR_TYPE_PALETTE			3
#define PNGU_COLOR_TYPE_RGB				4
#define PNGU_COLOR_TYPE_RGB_ALPHA		5
#define PNGU_COLOR_UNKNOWN 				6


// File attributes flags
#define PNGU_FATTRIB_READ_ONLY			0x01
#define PNGU_FATTRIB_HIDDEN				0x02
#define PNGU_FATTRIB_SYSTEM				0x04


#ifdef __cplusplus
	extern "C" {
#endif

// Types
typedef unsigned char PNGU_u8;
typedef unsigned short PNGU_u16;
typedef unsigned int PNGU_u32;
typedef unsigned long long PNGU_u64;

typedef struct
{
	PNGU_u32 imgWidth;
	PNGU_u32 imgHeight;
	PNGU_u32 imgBitDepth;
	PNGU_u32 imgColorType;
	#ifdef PNGU_USE_WII_SD
	PNGU_u32 fileSize;
	PNGU_u32 fileAttrib;
	#endif
} PNGUPROP;


// PNGU Interface //


// Macro to convert RGB8 values to RGB565
#define PNGU_RGB8_TO_RGB565(r,g,b) ( ((((PNGU_u16) r) & 0xF8U) << 8) | ((((PNGU_u16) g) & 0xFCU) << 3) | (((PNGU_u16) b) >> 3) )


// Macro to convert RGBA8 values to RGB5A3
#define PNGU_RGB8_TO_RGB5A3(r,g,b,a) ( ((PNGU_u16) (a ? 0x8000 : 0)) | ((((PNGU_u16) r) & 0xF8U) << 7) | ((((PNGU_u16) g) & 0xF8U) << 2) | (((PNGU_u16) b) >> 3) )


// Function to convert two RGB8 values to YCbYCr
PNGU_u32 PNGU_RGB8_TO_YCbYCr (PNGU_u8 r1, PNGU_u8 g1, PNGU_u8 b1, PNGU_u8 r2, PNGU_u8 g2, PNGU_u8 b2);


// Initialices PNGU. Always call (just one time) before using PNGU_* functions or macros defined below.
int PNGU_Init ();


// Frees PNGU resources. Call it when you are done with PNGU.
void PNGU_Release ();


// Selects a PNG file, previosly loaded into a buffer, for subsequent procesing.
int PNGU_SelectFileFromBuffer (const void *buffer);


// Selects a PNG file, from an SD card (Wii's front SD reader), for subsequent procesing.
#ifdef PNGU_USE_WII_SD
int PNGU_SelectFileFromWiiSD (const char *filename);
#endif


// Retrieves info from selected PNG file, including size, attributes, image dimensions and color format.
int PNGU_GetImageProperties (PNGUPROP *fileproperties);


// Expand selected PNG file into a YCbYCr buffer. You need to specify image dimensions, 
// buffer addres and stride (buffer width minus image width)
int PNGU_DecodeToYCbYCr (PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride);


// Macro for decoding an image inside a buffer at given coordinates.
#define PNGU_DECODE_TO_COORDS_YCbYCr(coordX,coordY,imgWidth,imgHeight,bufferWidth,bufferHeight,buffer)	\
																										\
		PNGU_DecodeToYCbYCr (imgWidth, imgHeight, buffer + coordY * bufferWidth * 2 +					\
							coordX * 2, bufferWidth-imgWidth)


// Expand selected PNG file into a RGBA8 buffer. You need to specify image dimensions, 
// buffer addres and default alpha value.
// WARNING: In current implementation source alpha, if present, is discarded. Alpha is always set to 'default_alpha'.
int PNGU_DecodeToRGBA8 (PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u8 default_alpha);


// Expand selected PNG file into a 4x4 RGB565 buffer. Image dimensions and buffer addres need to be
// specified. You can use decoded image as is to create a GX_TF_RGB565 texture.
int PNGU_DecodeTo4x4RGB565 (PNGU_u32 width, PNGU_u32 height, void *buffer);


// Expand selected PNG file into a 4x4 RGB565 buffer. Image dimensions and buffer addres need to be specified.
// You can use decoded image as is to create a GX_TF_RGB5A3 texture. 
// WARNING: In current implementation source alpha, if present, is discarded. Transparency bit is always set to 1.
int PNGU_DecodeTo4x4RGB5A3 (PNGU_u32 width, PNGU_u32 height, void *buffer);


// Expand selected PNG file into a 4x4 RGBA8 buffer. Image dimensions, buffer addres and default alpha value need 
// to be specified. You can use decoded image as is to create a GX_TF_RGBA8 texture. 
// WARNING: In current implementation source alpha, if present, is discarded. Alpha is always set to 'default_alpha'.
int PNGU_DecodeTo4x4RGBA8 (PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u8 default_alpha);


#ifdef __cplusplus
	}
#endif


#endif


